/**
 * 
 */
package info.niwota.home;

import instant.openvfs.home.Debug;
import instant.openvfs.home.Preferences;

import java.io.InputStream;
import java.util.Date;

import x.java.io.File;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebSettings;

/**
 * @author qiangli
 * 
 */
public class HomeTabFling extends HomeFlingActivity {

	private static final String TAG = "HomeTabFling";
	private boolean canSpeack;

	@Override
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		if (Debug.DEBUG) {
			Log.d(TAG, "onCreate savedInstance: " + savedInstance);
		}
	}

	@Override
	protected void initRes() {
		super.initRes();
		//
		mFlingView.setVisibility(View.VISIBLE);
		mGrid.setVisibility(View.GONE);
		//
		WebSettings s = mFlingViewPage.getSettings();
		s.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		s.setUseWideViewPort(false);
		s.setSavePassword(false);
		s.setSaveFormData(false);
		s.setSupportZoom(false);
		s.setBuiltInZoomControls(false);
		//
		s.setJavaScriptEnabled(false);
	}
	
	protected void initAdapter() {
	}
	
	@Override
	protected void initFling() {
		super.initFling();
	}

	@Override
	protected void initTtsMessages() {
		super.initTtsMessages();
	}

	@Override
	public void onDestroy() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onDestroy ");
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onPause ");
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (Debug.DEBUG) {
			Log.d(TAG, "onResume ");
		}
		canSpeack = Preferences.ttsEnabled(this);
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (Debug.DEBUG) {
			Log.d(TAG, "onSaveInstanceState: " + outState);
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		if (Debug.DEBUG) {
			Log.d(TAG, "onNewIntent: " + intent);
		}
		super.onNewIntent(intent);
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		//
		if (Debug.DEBUG) {
			Log.d(TAG, "onRestart: ");
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (Debug.DEBUG) {
			Log.d(TAG, "onStart: ");
		}
	}

	@Override
	protected void onStop() {
		if (Debug.DEBUG) {
			Log.d(TAG, "onStop: ");
		}
		super.onStop();
	}

	@Override
	protected boolean isFlinging() {
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (mFlingViewImage.getVisibility() == View.VISIBLE ||
				mFlingViewPage.getVisibility() == View.VISIBLE) {
			mFlingViewImage.setVisibility(View.GONE);
			mFlingViewPage.setVisibility(View.GONE);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void say(File f) {
		super.say(f);
		try {
			String ct = f.getContentType();
			if (ct != null && ct.startsWith("image/")) {
				InputStream is = f.getInputStream();
				Bitmap bm = BitmapFactory.decodeStream(is);
				is.close();
				mFlingViewImage.setImageBitmap(bm);
				mFlingViewImage.setVisibility(View.VISIBLE);
				mFlingViewPage.setVisibility(View.GONE);
			}  else {
				loadText(f, ct);
				mFlingViewImage.setVisibility(View.GONE);
				mFlingViewPage.setVisibility(View.VISIBLE);
			}
			mHeader.setText(f.getName());
		} catch (Throwable e) {
			sayError(e);
			e.printStackTrace();
		}
	}

	protected void loadText(File file, String mime) {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append("<html><body><table>");
			sb.append("<tr><td>Name:</td><td>"+file.getDisplayName()+"</td></tr>");
			sb.append("<tr><td>Path:</td><td>"+file.getPath()+"</td></tr>");
			sb.append("<tr><td>Size:</td><td>"+File.formatSize(file.length())+"</td></tr>");
			sb.append("<tr><td>Modified:</td><td>"+File.format(new Date(file.lastModified()))+"</td></tr>");
			sb.append("<tr><td>Type:</td><td>"+file.getContentType()+"</td></tr>");
			sb.append("</table></body></html>");
			mFlingViewPage.loadData(sb.toString(), "text/html", "UTF-8");
		} catch (Exception e) {
			sayError(e);
			e.printStackTrace();
		}
	}
	
	protected void speak(String s) {
		if (canSpeack) {
			super.speak(s);
		}
	}

	@Override
	protected File getContextFile(int pos) {
		return getSelectedFile();
	}
	
}
